package net.cctms.zhishu.webAppFramework.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;

import net.cctms.zhishu.R;
import net.cctms.zhishu.webAppFramework.adapter.ShareItemAdapter;


/**
 * 分享弹出菜单
 *
 * @author caot on 2015/5/20.
 */
public class SharePopupWindow extends PopupWindow {
    private LayoutInflater inflater;
    private Context context;
    private View contentView;
    private View rootView;
    private GridView shareItemGv;

    private boolean isOutAnim = false;
    private boolean isInAnim = false;
    private int position = -1;

    private ShareItemAdapter shareItemAdapter;
    /**
     * 微信朋友圈
     */
    public static final int WEIXIN_FRIENDS = 0;
    /**
     * 微信好友
     */
    public static final int WEXIN = 1;
    /**
     * qq空间
     */
    public static final int QZONE = 2;
    /**
     * qq好友
     */
    public static final int QQ = 3;
    /**
     * 微博
     */
    public static final int WEIBO = 4;
    /**
     * 短信
     */
    public static final int SMS = 5;
    /**
     * 小程序
     */
    public static final int MINI = 6;

    private boolean isShareActivity = false;

    private boolean isInVisibleSms = true;

    private OnShareItemClickListener onShareItemClickListener;

    public SharePopupWindow(Context context) {
        //super(context);
        this.context = context;
        initView();
    }

    public SharePopupWindow(Context context, boolean isShareActivity) {
        //super(context);
        this.context = context;
        this.isShareActivity = isShareActivity;
        initView();
    }

    public void setOnItemClickListener(OnShareItemClickListener onShareItemClickListener) {
        this.onShareItemClickListener = onShareItemClickListener;
    }

    private void initView() {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate( R.layout.share_window_layout, null);
        //rootView = contentView.findViewById(R.id.share_popup_window_root_view);
        shareItemGv = (GridView) contentView.findViewById(R.id.share_window_item_gv);
        shareItemGv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        shareItemAdapter = new ShareItemAdapter(context);
        shareItemAdapter.setIsInVisibleSms(true);
        shareItemGv.setAdapter(shareItemAdapter);
        rootView.setFocusable(true);
        rootView.setFocusableInTouchMode(true);
        setContentView(contentView);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setOutsideTouchable(true);
        initListener();
        update();
        //设置弹出遮罩
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.common_alpha_50));
        setBackgroundDrawable(dw);
        //点击窗体窗体消失
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initListener() {
        shareItemGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (isInVisibleSms && position == SMS) {
                    return;
                }
                dismiss();
                if (onShareItemClickListener != null) {
                    onShareItemClickListener.onShareItemClick(position);
                }
            }
        });
    }

    public interface OnShareItemClickListener {
        void onShareItemClick(int position);
    }

}
